package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bargainexception.PoorException;
import com.qf.bean.Connector;
import com.qf.bean.Follow;
import com.qf.bean.Imp;
import com.qf.enums.CustomerEnum;
import com.qf.service.ConnectorService;
import com.qf.service.FollowService;
import com.qf.service.ImpService;

import com.qf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.validation.Valid;
import java.util.List;

import static com.qf.constant.BargainConstant.SIZE;

@Controller
@RequestMapping("/connector")
public class ConnectorController {

    @Autowired
    private ConnectorService connectorService;

    @Autowired
    private ImpService impService;

    @Autowired
    private FollowService followService;

    //查询全部
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(defaultValue = "1")Integer page,
                          String charityname,String name,
                          @RequestParam(defaultValue = "-1") Integer impgrade,
                          Model model){
        PageInfo<Connector> pageInfo=connectorService.findAll(page,SIZE,charityname,name,impgrade);
        List<Imp> impList=impService.findAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("impList",impList);
        model.addAttribute("charityname",charityname);
        model.addAttribute("name",name);
        model.addAttribute("impgrade",impgrade);
        return "connector/list";
    }

    //查询详细信息
    @RequestMapping("/info")
    public String info(Model model,Integer id){
        Connector connector=connectorService.findOne(id);
        model.addAttribute("connector",connector);
        return "connector/info";
    }

    //跳转到添加页面
    @RequestMapping("/add-ui")
    public String addUI(Model model){
        List<Follow> followList=followService.findAll();
        List<Imp> impList = impService.findAll();
        model.addAttribute("followList",followList);
        model.addAttribute("impList",impList);
        return "connector/add";
    }

    //执行添加
    @RequestMapping("/addConnector")
    @ResponseBody
    public ResultVO addConnector(@Valid Connector connector, BindingResult bindingResult){

        if (connector.getImpgrade()<0){
            return new ResultVO(51,"请选择重要程度",null);
        }
        if (connector.getFollowstatus()<0){
            return new ResultVO(52,"请选择跟进状态",null);
        }

        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new PoorException(CustomerEnum.PARAM_ERROR.getCode(),message);
        }

        //执行添加
        connectorService.addConnector(connector);
        return new ResultVO(0,"",null);
    }

    //跳转到修改页面
    @RequestMapping("/update-ui")
    public String updateUI(Integer id,Model model){
        Connector connector = connectorService.findOne(id);
        List<Follow> followList=followService.findAll();
        List<Imp> impList = impService.findAll();
        model.addAttribute("connector",connector);
        model.addAttribute("followList",followList);
        model.addAttribute("impList",impList);
        return "connector/update";
    }
}
